Ansible : Use Playbook#1
2015/04/22 |
This is the basic usage of Ansible Playbook.
Playbook is written as YAML file.
|
|
[1] | For example, create a Playbook which a file exists with the same permission. |
[cent@dlp ~]$
vi playbook_sample.yml # target hostname or group name - hosts: target_servers # define tasks tasks: # task name (any name you like) - name: Test Task # use file module to set the file state file: path=/home/cent/test.conf state=touch owner=cent group=cent mode=0600 # run Playbook [cent@dlp ~]$ ansible-playbook playbook_sample.yml PLAY [target_servers] ********************************************************* GATHERING FACTS *************************************************************** ok: [10.0.0.52] ok: [10.0.0.51] TASK: [Test Task] ************************************************************* changed: [10.0.0.51] changed: [10.0.0.52] PLAY RECAP ******************************************************************** 10.0.0.51 : ok=2 changed=1 unreachable=0 failed=0 10.0.0.52 : ok=2 changed=1 unreachable=0 failed=0 # confirm settings [cent@dlp ~]$ ansible target_servers -m command -a "ls -l /home/cent" 10.0.0.51 | success | rc=0 >> total 0 -rw------- 1 cent cent 0 Apr 21 15:35 test.conf 10.0.0.52 | success | rc=0 >> total 0 -rw------- 1 cent cent 0 Apr 21 15:35 test.conf |
[2] | For example, create a Playbook which Apache httpd is installed and running. |
[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
# use priviledge (default : root) become: yes # the way to use priviledge become_method: sudo # define tasks tasks: - name: httpd is installed yum: name=httpd state=installed
- name: httpd is running and enabled
service: name=httpd state=started enabled=yes # run Playbook [cent@dlp ~]$ ansible-playbook playbook_sample.yml --ask-become-pass SUDO password: PLAY [target_servers] ********************************************************* GATHERING FACTS *************************************************************** ok: [10.0.0.51] ok: [10.0.0.52] TASK: [httpd is installed] **************************************************** changed: [10.0.0.51] changed: [10.0.0.52] TASK: [httpd is running and enabled] ****************************************** changed: [10.0.0.51] changed: [10.0.0.52] PLAY RECAP ******************************************************************** 10.0.0.51 : ok=3 changed=2 unreachable=0 failed=0 10.0.0.52 : ok=3 changed=2 unreachable=0 failed=0 # confirm settings [cent@dlp ~]$ ansible target_servers -m command -a "/sbin/service httpd status" -b --ask-become-pass SUDO password: 10.0.0.51 | success | rc=0 >> httpd (pid 1739) is running... 10.0.0.52 | success | rc=0 >> httpd (pid 1673) is running... |